/**
 *
 *
 *
 *
 * 版权所有，侵权必究！
 */

package com.base.commons.tools.security.user;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.base.commons.tools.constant.Constant;
import com.base.commons.tools.utils.HttpContextUtils;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.net.URLDecoder;

/**
 * 用户
 *
 * @author carl
 * @since 1.0.0
 */
public class SecurityUser {

    /**
     * 获取用户信息
     */
    @SneakyThrows
    public static UserDetail getUser() {
        HttpServletRequest request = HttpContextUtils.getHttpServletRequest();
        if(request == null){
            return null;
        }
        if(StringUtils.isBlank(request.getHeader(Constant.USER))){
            return null;
        }
        String userStr = URLDecoder.decode(request.getHeader(Constant.USER), "UTF-8");
        if(StringUtils.isBlank(userStr)){
            return null;
        }
        return  JSON.parseObject(userStr,new TypeReference<UserDetail>(){});
    }




}
